package com.guigusuqi.hy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.guigusuqi.app.entity.LoginWxUser;
import com.guigusuqi.app.entity.WxUser;
import com.guigusuqi.commonutils.utils.GetTokenByAuthorizationUtils;
import com.guigusuqi.commonutils.utils.RedisCache;
import com.guigusuqi.commonutils.vo.Result;
import com.guigusuqi.hy.dto.Referral;
import com.guigusuqi.hy.entity.Recommend;
import com.guigusuqi.hy.mapper.RecommendMapper;
import com.guigusuqi.hy.service.IntegralAdminService;
import com.guigusuqi.hy.service.RecommendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigusuqi.hy.vo.RecommendVo;
import com.guigusuqi.hy.vo.ReferenceVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 新人推荐管理 服务实现类
 * </p>
 *
 * @author suqi
 * @since 2022-04-19
 */
@Service
public class RecommendServiceImpl extends ServiceImpl<RecommendMapper, Recommend> implements RecommendService
{
    @Resource
    private RecommendMapper recommendMapper;

    @Resource
    private RedisCache redisCache;

    @Resource
    private IntegralAdminService integralAdminService;

    @Override
    public Result RecommendNew(Referral referral) throws IOException
    {
        String token = GetTokenByAuthorizationUtils.getToken();
        LoginWxUser loginWxUser = redisCache.getCacheObject("TOKEN_"+token);
        WxUser user = loginWxUser.getUser();
        String openid = user.getOpenid();

        Recommend recommend = new Recommend();

        recommend.setOpenid(openid);
        recommend.setNickname(referral.getNickname());
        recommend.setPhone(referral.getMobile());
        recommend.setSex(referral.getSex());

        recommend.setReferralName(referral.getReferralName());
        recommend.setReferralMobile(referral.getReferralMobile());
        recommend.setReferralSex(referral.getReferralSex());
        recommend.setRecomDetail(referral.getDetail());
        recommend.setHasChecked("0");
        recommend.setRecomDate(new Date());
        // 判断是否重复推荐同一个手机号
        LambdaQueryWrapper<Recommend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Recommend::getOpenid,openid);
        List<Recommend> recommendList = recommendMapper.selectList(queryWrapper);
        for (Recommend recommend1 : recommendList)
        {
            if (StringUtils.equals(recommend1.getReferralMobile(),referral.getReferralMobile()))
            {
                return Result.fail().message("不允许重复推荐一个手机号！");
            }
        }

        boolean save = save(recommend);
        if(save)
        {
            List<RecommendVo> recommendVos = recommendList();
            return Result.success().message("推荐成功，后台审核添加积分").data("rows",recommendVos);
//            List<RecommendVo> recommendVos = recommendList();
//            Result result = integralAdminService.addIntegral("1");
//
//            if(result.getCode() == 200)
//            {
//                return Result.success().message("推荐成功，后台审核添加积分").data("rows",recommendVos);
//            }else
//            {
//                return Result.success().message("推荐成功,"+result.getMessage()).data("rows",recommendVos);
//            }
        }else
        {
            return Result.fail().message("推荐失败！");
        }
    }

    @Override
    public List<RecommendVo> recommendList() throws IOException
    {
        String token = GetTokenByAuthorizationUtils.getToken();
        LoginWxUser loginWxUser = redisCache.getCacheObject("TOKEN_"+token);
        WxUser user = loginWxUser.getUser();
        String openid = user.getOpenid();

        LambdaQueryWrapper<Recommend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Recommend::getOpenid,openid);

        List<Recommend> list = list(queryWrapper);
        List<RecommendVo> recommendVos = new ArrayList<>();
        for (Recommend recommend : list)
        {
            RecommendVo recommendVo = new RecommendVo();
            recommendVo.setReferralName(recommend.getReferralName());
            recommendVo.setReferralMobile(recommend.getReferralMobile());
            recommendVo.setRecomDate(recommend.getRecomDate());
            recommendVo.setRecomDetail(recommend.getRecomDetail());
            recommendVo.setHasChecked(recommend.getHasChecked());
            recommendVos.add(recommendVo);
        }
        return recommendVos;
    }

    @Override
    public Result referralList() throws IOException
    {
        String token = GetTokenByAuthorizationUtils.getToken();
        LoginWxUser loginWxUser = redisCache.getCacheObject("TOKEN_"+token);
        WxUser user = loginWxUser.getUser();

        String phone = user.getPhone();
        String nickname = user.getNickname();
        Integer sex = user.getSex();

        LambdaQueryWrapper<Recommend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Recommend::getReferralName,nickname).eq(Recommend::getReferralMobile,phone).eq(Recommend::getReferralSex,sex);

        List<Recommend> recommends = recommendMapper.selectList(queryWrapper);
        List<ReferenceVo> referenceVoList = new ArrayList<>();
        for (Recommend recommend : recommends)
        {
            ReferenceVo referenceVo = new ReferenceVo();
            referenceVo.setNickname(recommend.getNickname());
            referenceVo.setPhone(recommend.getPhone());
            referenceVoList.add(referenceVo);
        }
        return Result.success().data("rows", referenceVoList);
    }
}
